<template>
  <div class="reset-password-bg">
    <el-card class="reset-password-card">
      <h2>重置密码</h2>
      <el-form
        ref="resetForm"
        :model="resetFormData"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="resetFormData.email"
            placeholder="请输入注册邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="resetFormData.newPassword"
            type="password"
            placeholder="请输入新密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="resetFormData.confirmPassword"
            type="password"
            placeholder="请再次输入新密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-button type="primary" @click="handleReset">提交</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import requestUtil from "@/util/request";

const router = useRouter();

// 表单引用
const resetForm = ref();

// 表单数据
const resetFormData = reactive({
  email: "",
  newPassword: "",
  confirmPassword: "",
});

// 表单验证规则
const rules = reactive({
  email: [
    { required: true, message: "请输入邮箱", trigger: "blur" },
    { type: "email", message: "请输入正确的邮箱格式", trigger: "blur" },
  ],
  newPassword: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    { min: 6, max: 20, message: "密码长度在 6 到 20 个字符", trigger: "blur" },
  ],
  confirmPassword: [
    { required: true, message: "请再次输入密码", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (value !== resetFormData.newPassword) {
          callback(new Error("两次输入密码不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
});

// 重置密码处理
const handleReset = async () => {
  try {
    // 表单验证
    const valid = await resetForm.value.validate();
    if (!valid) return;

    // 发送重置密码请求
    const response = await requestUtil.post("/user/resetPassword", {
      email: resetFormData.email,
      newPassword: resetFormData.newPassword,
    });

    if (response.data.code === 200) {
      ElMessage.success("密码重置成功，请重新登录");
      // 跳转到登录页面
      router.push("/login");
    } else {
      ElMessage.error(response.data.message || "重置密码失败");
    }
  } catch (error) {
    console.error("重置密码错误:", error);
    ElMessage.error("重置密码失败，请重试");
  }
};
</script>

<style scoped>
.reset-password-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.reset-password-card {
  width: 100%;
  max-width: 400px;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.reset-password-card h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-weight: 600;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  border-radius: 8px;
  margin-top: 20px;
}

.el-input {
  border-radius: 8px;
}

.el-input__inner {
  height: 45px;
  line-height: 45px;
  border-radius: 8px;
}
</style>
